package com.zdlog.wenjuan.service.impl;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.dao.FormInfoRepository;
import com.zdlog.wenjuan.dao.MainProblemRepository;
import com.zdlog.wenjuan.dao.ProblemDetailRepository;
import com.zdlog.wenjuan.dao.ProblemOptionRepository;
import com.zdlog.wenjuan.dataobject.FormInfo;
import com.zdlog.wenjuan.dataobject.MainProblem;
import com.zdlog.wenjuan.dataobject.ProblemDetail;
import com.zdlog.wenjuan.dataobject.ProblemOption;
import com.zdlog.wenjuan.dto.web.FormDTO;
import com.zdlog.wenjuan.dto.web.MainProblemDTO;
import com.zdlog.wenjuan.dto.web.ProblemDetailDTO;
import com.zdlog.wenjuan.dto.web.ProblemOptionDTO;
import com.zdlog.wenjuan.enums.FormStatusEnum;
import com.zdlog.wenjuan.enums.ProblemTypeEnum;
import com.zdlog.wenjuan.service.FormService;
import com.zdlog.wenjuan.service.MainProblemService;
import com.zdlog.wenjuan.service.WebSocket;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.service.impl
 * @author: Kuaile
 * @create: 2018-02-05 00:08
 **/
@Service
public class FormServiceImpl implements FormService{

    @Autowired
    private FormInfoRepository repository;

    /**
     * 分支service
     */
    @Autowired
    private MainProblemService service;

    /**
     * 分支dao
     */
    @Autowired
    private MainProblemRepository mainProblemRepository;

    /**
     * 问题dao
     */
    @Autowired
    private ProblemDetailRepository detailRepository;

    /**
     * 选项dao
     */

    @Autowired
    private ProblemOptionRepository optionRepository;


    @Autowired
    private WebSocket webSocket;



    @Override
    public Page<FormDTO> list(String userId,Pageable pageable) {
        List<FormDTO> formDTOList = new ArrayList<>();
        Page<FormInfo> formInfoPage = repository.findByUserId(userId, pageable);
        List<FormInfo> formInfoList =formInfoPage.getContent();
        FormDTO formDTO = null;
        for (FormInfo formInfo : formInfoList) {
            formDTO = new FormDTO();
            BeanUtils.copyProperties(formInfo, formDTO);
            if (formInfo.getStatus()==FormStatusEnum.STOP.getCode()) {
                formDTO.setStatus(FormStatusEnum.STOP.getMessage());
            } else if (formInfo.getStatus()==FormStatusEnum.START.getCode()){
                formDTO.setStatus(FormStatusEnum.START.getMessage());
            }
            formDTO.setMainProblemNumber(service.list(formInfo.getFormId()).size());
            formDTOList.add(formDTO);
        }

        return new PageImpl<FormDTO>(formDTOList, pageable, formInfoPage.getTotalElements());

    }

    @Override
    public FormInfo findId(String id) {

        return repository.findByFormId(id);

    }

    @Override
    public FormInfo save(FormInfo formInfo) {
       return repository.save(formInfo);
    }

    @Override
    public FormInfo updata(FormInfo formInfo) {
       return repository.save(formInfo);
    }


    /**
     * 管理后台问卷详情
     * @param formId
     * @return
     */
    @Override
    public ResultBoolenVO detail(String formId) {
        FormDTO formDTO = new FormDTO();
        MainProblemDTO mainProblemDTO = null;
        ProblemDetailDTO problemDetailDTO = null;
        ProblemOptionDTO problemOptionDTO = null;
        FormInfo formInfo = repository.findByFormId(formId);
        BeanUtils.copyProperties(formInfo, formDTO);
        List<MainProblem> mainProblemList = mainProblemRepository.findByFormIdOrderBySerialNumberAsc(formId);
        for (MainProblem mainProblem : mainProblemList) {
            mainProblemDTO = new MainProblemDTO();
            BeanUtils.copyProperties(mainProblem, mainProblemDTO);
            List<ProblemDetail> problemDetailList = detailRepository.findByMainProblemIdOrderBySerialNumberAsc(mainProblem.getMainProblemId());
            for (ProblemDetail problemDetail : problemDetailList) {
                problemDetailDTO = new ProblemDetailDTO();
                if (problemDetail.getProblemType() != ProblemTypeEnum.PINGFENG.getCode() && problemDetail.getProblemType() != ProblemTypeEnum.WENZI.getCode()) {
                    BeanUtils.copyProperties(problemDetail, problemDetailDTO);
                    List<ProblemOption> problemOptionList = optionRepository.findByProblemIdOrderBySerialNumberAsc(problemDetail.getProblemDetailId());
                    for (ProblemOption problemOption : problemOptionList) {
                        problemOptionDTO = new ProblemOptionDTO();
                        BeanUtils.copyProperties(problemOption, problemOptionDTO);
                        problemDetailDTO.problemOptionDTOList.add(problemOptionDTO);
                    }
                    mainProblemDTO.problemDetailDTOList.add(problemDetailDTO);
                } else {
                    BeanUtils.copyProperties(problemDetail, problemDetailDTO);
                    mainProblemDTO.problemDetailDTOList.add(problemDetailDTO);
                }
            }
            formDTO.mainProblemDTOList.add(mainProblemDTO);
        }
        if (formDTO.mainProblemDTOList.size() != 0) {
            return ResultBoolenVOUtil.success(formDTO);
        }
        return ResultBoolenVOUtil.error("查询失败");
    }

    @Override
    @Transactional
    public ResultBoolenVO delete(String formId) {
        FormInfo formInfo = repository.findByFormId(formId);
        List<MainProblem> mainProblemList = mainProblemRepository.findByFormIdOrderBySerialNumberAsc(formId);
        for (MainProblem mainProblem : mainProblemList) {
            List<ProblemDetail> problemDetailList = detailRepository.findByMainProblemIdOrderBySerialNumberAsc(mainProblem.getMainProblemId());
            for (ProblemDetail problemDetail : problemDetailList) {
                if (problemDetail.getProblemType() != ProblemTypeEnum.PINGFENG.getCode() && problemDetail.getProblemType() != ProblemTypeEnum.WENZI.getCode()) {
                    List<ProblemOption> problemOptionList = optionRepository.findByProblemIdOrderBySerialNumberAsc(problemDetail.getProblemDetailId());
                    for (ProblemOption problemOption : problemOptionList) {
                        optionRepository.delete(problemOption.getOptionId());
                    }
                }
                detailRepository.delete(problemDetail.getProblemDetailId());
            }
            mainProblemRepository.delete(mainProblem.getMainProblemId());
        }
        repository.delete(formInfo.getFormId());
        return ResultBoolenVOUtil.success();
    }

    @Override
    public List<FormInfo> findName(String formName, Integer status) {

        if (formName == null||formName.equals("")) {
            return repository.findByStatusOrderByCreateTimeAsc(status);
        }
        return repository.findByFormNameLikeAndStatusOrderByCreateTimeDesc('%'+formName+'%',status);
    }




    @Override
    public com.zdlog.wenjuan.dto.android.FormDTO findById(String formId) {
        com.zdlog.wenjuan.dto.android.FormDTO formDTO = new com.zdlog.wenjuan.dto.android.FormDTO();
        FormInfo formInfo = repository.findByFormId(formId);
        BeanUtils.copyProperties(formInfo, formDTO);
        List<MainProblem> mainProblemList = mainProblemRepository.findByFormIdOrderBySerialNumberAsc(formId);
        if (mainProblemList.size() != 0) {
            List<com.zdlog.wenjuan.dto.android.MainProblemDTO> mainProblemDTOList = new ArrayList<>();
            com.zdlog.wenjuan.dto.android.MainProblemDTO mainProblemDTO = null;
            for (MainProblem mainProblem : mainProblemList) {
                mainProblemDTO = new com.zdlog.wenjuan.dto.android.MainProblemDTO();
                BeanUtils.copyProperties(mainProblem, mainProblemDTO);
                List<ProblemDetail> problemDetailList = detailRepository.findByMainProblemIdOrderBySerialNumberAsc(mainProblem.getMainProblemId());
                if (problemDetailList.size() != 0) {
                    List<com.zdlog.wenjuan.dto.android.ProblemDetailDTO> problemDetailDTOList = new ArrayList<>();
                    com.zdlog.wenjuan.dto.android.ProblemDetailDTO problemDetailDTO = null;
                    for (ProblemDetail problemDetai : problemDetailList) {
                        problemDetailDTO = new com.zdlog.wenjuan.dto.android.ProblemDetailDTO();
                        BeanUtils.copyProperties(problemDetai, problemDetailDTO);
                        if (problemDetai.getProblemType() == ProblemTypeEnum.DANXUAN.getCode() || problemDetai.getProblemType() == ProblemTypeEnum.DUOXUAN.getCode()) {
                            List<ProblemOption> problemOptionList = optionRepository.findByProblemIdOrderBySerialNumberAsc(problemDetai.getProblemDetailId());
                            if (problemOptionList.size() != 0) {
                                List<com.zdlog.wenjuan.dto.android.ProblemOptionDTO> problemOptionDTOList = new ArrayList<>();
                                com.zdlog.wenjuan.dto.android.ProblemOptionDTO problemOptionDTO = null;
                                for (ProblemOption problemOption : problemOptionList) {
                                    problemOptionDTO = new com.zdlog.wenjuan.dto.android.ProblemOptionDTO();
                                    BeanUtils.copyProperties(problemOption,problemOptionDTO);
                                    problemOptionDTOList.add(problemOptionDTO);
                                }
                                problemDetailDTO.setOptions(problemOptionDTOList);
                            }
                        }
                        problemDetailDTOList.add(problemDetailDTO);
                    }
                    mainProblemDTO.setProblemDetails(problemDetailDTOList);
                }
                mainProblemDTOList.add(mainProblemDTO);
            }
            formDTO.setMainProblems(mainProblemDTOList);
        }

        return formDTO;

    }

}
